package com.hsmw.api.controller.driver;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.mapper.*;
import com.hsmw.api.service.HsmwDriverUserService;
import com.hsmw.api.service.HsmwExchangeBusService;
import com.hsmw.api.vo.DriverInfo;
import com.htn.common.core.base.BusinessType;
import com.htn.common.core.base.DataResponse;
import com.hsmw.common.base.Log;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.hsmw.common.base.BaseController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tjy
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/hswm/bus")
@Api(tags = "换车信息表")
public class HsmwExchangeBusController extends BaseController<HsmwExchangeBusService, HsmwExchangeBus> {


    @Autowired
    private HsmwExchangeBusMapper hsmwExchangeBusMapper;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;

    @Autowired
    private HsmwUserMapper hsmwUserMapper;

    @Autowired
    private HsmwVehicleMapper hsmwVehicleMapper;

    @Autowired
    private HsmwExchangeBusService hsmwExchangeBusService;

    @Autowired
    private HsmwExchangeBusHistoryMapper hsmwExchangeBusHistoryMapper;


    @Autowired
    private HsmwDriverUserMapper hsmwDriverUserMapper;

    @ApiOperation(value = "根据司机id，车辆id查询换车的信息", notes = "根据司机id，车辆id查询换车的信息", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "车俩id",name = "id",dataType = "String"),
            @ApiImplicitParam(value = "司机id",name = "driId",dataType = "String")
    })
    @PostMapping("findExchangeBusInfoById")
    public DataResponse findExchangeBusInfoById(String id,String driId){

        HsmwExchangeBus hsmwExchangeBus = hsmwExchangeBusMapper.selectOne(new LambdaQueryWrapper<HsmwExchangeBus>().eq(HsmwExchangeBus::getDriverId, driId).eq(HsmwExchangeBus::getApplyStatus,"0"));
        if(hsmwExchangeBus != null){
            if(StringUtils.isNotBlank(hsmwExchangeBus.getVehicleId())){
                HsmwVehicle hsmwVehicle = hsmwVehicleMapper.selectById(hsmwExchangeBus.getVehicleId());
                hsmwExchangeBus.setVehicleModel(hsmwVehicle.getVehicleModel());
                hsmwExchangeBus.setVehiclePlate(hsmwVehicle.getVehiclePlate());
                hsmwExchangeBus.setVehicleType(hsmwVehicle.getVehicleType());
            }
        }
        return new DataResponse(hsmwExchangeBus);
    }


    @Log(title = "司机端：根据司机id查询换车的信息", businessType = BusinessType.QUERY)
    @ApiOperation(value = "根据司机id查询换车的信息", notes = "根据司机id，车辆id查询换车的信息", produces = "application/json")
    @ApiImplicitParam(value = "司机id", name = "driId", dataType = "String")
    @PostMapping("findExchangeBusInfoBydriId")
    public DataResponse findExchangeBusInfoBydriId(String driId) {
        List<DriverInfo> list = new ArrayList<>();
        List<HsmwExchangeBus> hsmwExchangeBus = hsmwExchangeBusMapper.selectList(new LambdaQueryWrapper<HsmwExchangeBus>().eq(HsmwExchangeBus::getExchangeDriverId, driId).eq(HsmwExchangeBus::getApplyStatus,"0"));
        for (HsmwExchangeBus exchangeBus : hsmwExchangeBus) {
            String driverId = exchangeBus.getDriverId();
            HsmwUserDriver hsmwUserDriver = hsmwUserDriverMapper.selectById(driverId);
            String userId = hsmwUserDriver.getUserId();
            HsmwDriverUser hsmwUser = hsmwDriverUserMapper.selectById(userId);
            DriverInfo driverInfo = new DriverInfo();
            driverInfo.setDriverUsername(hsmwUser.getUsername());
            driverInfo.setName(hsmwUser.getName());
            driverInfo.setImages(hsmwUserDriver.getMyPicture());
            driverInfo.setVehicleNum(hsmwUserDriver.getId());
            driverInfo.setStatus(hsmwUserDriver.getWorkStatus());
            driverInfo.setVehicleId(hsmwUserDriver.getVehicleId());
            list.add(driverInfo);
        }
        return new DataResponse(list);
    }


    @Log(title = "司机端：取消申请换车", businessType = BusinessType.DELETE)
    @ApiOperation(value = "取消申请换车", notes = "取消申请换车", produces = "application/json")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @PostMapping("deleteExchangeBusBydriId")
    public DataResponse deleteExchangeBusBydriId(String id){

        HsmwExchangeBus hsmwExchangeBus = hsmwExchangeBusService.getById(id);
        HsmwExchangeBusHistory hsmwExchangeBusHistory = new HsmwExchangeBusHistory();
        BeanUtils.copyProperties(hsmwExchangeBus,hsmwExchangeBusHistory);
        hsmwExchangeBusHistoryMapper.insert(hsmwExchangeBusHistory);
        boolean remove = hsmwExchangeBusService.removeById(id);

        if(remove){
            return new DataResponse(CommonConstants.ResponseStatus.SUCCESS);
        }
        return new DataResponse(CommonConstants.ResponseStatus.FAIL);
    }


}

